home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Japanese
/
cpsimage.cab
/
data
/
sys
/
XIPRunClass.elf
< prev
next >
Wrap
Text File
|
2009-04-23
|
19KB
|
602 lines
/* $Id: XIPRunClass.elf,v 1.7 2009/03/03 21:44:41 kingsley Exp $ */
/* This file contains script to parse xml that contains a script (either by
** name or the actual script) and parameters. The script begins near the end
** of the file. Currently this supports two schemas that define the XML
** document to parse.
*/
#load "sys/xipxml.elf";
/*****************************************************************************/
/*
** @New This class implements the parser for the RunXIP schema. The root
** contains an input element which contains a script element and a
** params element. The script element may name a script file or it
** contain the actual script.
*/
/* @setVocal Output messages as XML is parsed. */
/* @parseXPath Given an XPath object, parse the XML and initialize this. */
/* @getReturnXML Get an XML document with the results of script execution. */
/* @getScriptName What is the name of the script to execute? */
/* @getParamList Get a LIST that contains the import values for the script. */
/* @getReturnList Get a LIST that contains the returns values for the script. */
/*****************************************************************************/
private
CLASS XIPRunClass
{
METHOD New() {}
METHOD setVocal (BOOLEAN setTo) {
this.isVocal = setTo;
}
METHOD parseXPath ( XPath xpath ) RETURNS ( BOOLEAN success )
{
if (this.isVocal == TRUE) print "Initialize XIPRunClass via XPath";
success = this.parseScript( xpath: xpath );
if (success == FALSE)
{
if (this.isVocal == TRUE) print "Failed in parseScript";
this.isValid = FALSE;
return;
}
success = this.parseParams( xpath: xpath );
if (success == FALSE)
{
if (this.isVocal == TRUE) print "Failed in parseParams";
this.isValid = FALSE;
return;
}
success = this.parseReturns( xpath: xpath );
if (success == FALSE)
{
if (this.isVocal == TRUE) print "Failed in parseReturns";
this.isValid = FALSE;
return;
}
this.isValid = TRUE;
if (this.isVocal == TRUE) print "Initialization successful";
}
METHOD getReturnXML ( LIST exportList )
RETURNS ( STRING rXml, BOOLEAN erCatch )
{
if (this.isVocal == TRUE) print "XIPRunClass:getReturnXML";
XmlDocumentBuilder rtdb;
XmlDocument rtdoc = rtdb.newDocument();
XmlElement rtroot = rtdoc.createElement( name: "RunXIP" );
rtdoc.setDocumentElement( node: rtroot );
XmlElement rtout = rtdoc.createElement( name: "Output" );
rtroot.appendChild ( node: rtout );
INTEGER icnt = this.retTypes.length();
INTEGER i;
STRING rtype;
STRING rname;
OBJECT eobj;
erCatch = FALSE;
// get error and warning messages - always add these
i = exportList.search(name: "runxipError");
if (i != -1) {
erCatch = TRUE;
eobj = exportList.ref( entry: i );
this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
name: "runxipError", datatype: "String" );
}
i = exportList.search(name: "runxipWarning");
if (i != -1) {
erCatch = TRUE;
eobj = exportList.ref( entry: i );
this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
name: "runxipWarning", datatype: "String" );
}
for ( i=0; i<icnt; i++ )
{
rname = this.retNames.ref( entry: i );
rtype = this.retTypes.ref( entry: i );
eobj = exportList.ref( name: rname );
if ((eobj) || (rtype == "Integer"))
this.addReturnValXML( xmlDoc: rtdoc, outEle: rtout, obj: eobj,
name: rname, datatype: rtype );
}
if (this.isVocal == TRUE) {
print "Returning Document is:";
print rtdoc.dumpString( format: 1 );
}
rXml = rtdoc.dumpString( format: 0 );
}
METHOD getScriptName () RETURNS ( STRING name )
{
name = this.name;
}
METHOD getParamList () RETURNS ( LIST params )
{
params = this.params;
}
METHOD getReturnList () RETURNS ( LIST retvals )
{
retvals = this.retVals;
}
METHOD getIsValid () RETURNS ( BOOLEAN iv )
{
iv = this.isValid;
}
METHOD getScriptValue () RETURNS ( STRING sv )
{
sv = this.scriptValue;
}
METHOD getIsBlock () RETURNS ( BOOLEAN ib )
{
ib = this.isBlock;
}
/*****************************************************************************
******************** Lots of Private Methods ******************************
*****************************************************************************/
private METHOD addReturnValXML( XmlDocument xmlDoc, XmlElement outEle, OBJECT obj,
STRING name, STRING datatype )
{
XmlAttr rtAttr;
XmlElement rtelm = xmlDoc.createElement( name: "ReturnVal" );
XmlElement valName = xmlDoc.createElement( name: "Name" );
//XmlElement valVal = xmlDoc.createElement( name: "Value" );
STRING objAsStr = obj;
/* Add the ArgType attribute to the ReturnVal element */
rtAttr = xmlDoc.createAttribute(name: "ArgType");
rtAttr.setValue( value: datatype );
rtelm.setAttributeNode( node: rtAttr );
/* Add a child element that contains the Val's name */
valName.setNodeValue( value: name );
rtelm.appendChild ( node: valName );
/* Add a child element that contains the actual value */
LIST list;
if (obj.Type() == "LIST")
{
list = obj;
rtelm.setAttribute(name: "IsList", value: "true");
}
else
{
list.insert(obj: obj);
}
this.addReturnValXmlValues(xmlDoc: xmlDoc, rtelm: rtelm, list: list);
/* put the new element (ReturnVal) into the Output element */
outEle.appendChild ( node: rtelm );
}
private METHOD addReturnValXmlValues(XmlDocument xmlDoc, XmlElement rtelm, LIST list)
{
INTEGER i;
XmlElement valVal;
STRING name;
STRING str;
XmlAttr valAttr;
for(i = 0; i < list.length(); i++)
{
name = list.name(entry: i);
` str = list[i];
valVal = xmlDoc.createElement( name: "Value" );
valVal.setNodeValue( value: str );
valVal.setAttribute(name: "Name", value: name);
rtelm.appendChild ( node: valVal );
}
}
private METHOD parseScript ( XPath xpath ) RETURNS ( BOOLEAN success )
{
// Example
// <Name>My Script</Name>
// <Type>block</Type>
// <Value>yada, yada</Value>
/* Test for the existance of script element */
XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/Script" );
if (!nset || (nset.getLength() < 1) )
{
SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script element in XPath" );
success = FALSE;
this.isValid = FALSE;
return;
}
/* Test for the existance of script name...not required, use first one - index 0 */
nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Name" );
if (nset && (nset.getLength() > 0) ) {
this.name = this.innerText( xelem: nset.item( index: 0) );
}
nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Type" );
if (!nset || (nset.getLength() < 1) )
{
SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script/type element in XPath" );
success = FALSE;
this.isValid = FALSE;
return;
}
this.isBlock = this.innerText( xelem: nset.item( index: 0) ).strcasecmp( str: "block" );
nset = xpath.evaluate( expr: "/RunXIP/Input/Script/Value" );
if (!nset || (nset.getLength() < 1) )
{
SetStatus( op: "stop", msg: "XIPRunClass: Unable to find Script/Value element in XPath" );
success = FALSE;
this.isValid = FALSE;
return;
}
this.scriptValue = this.innerText( xelem: nset.item( index: 0) );
success = TRUE;
}
private METHOD parseParams ( XPath xpath ) RETURNS ( BOOLEAN success )
{
// Example
// <Param ArgType="Integer" IsList="true">
// <Name>aParam</Name>
// <Value>27</Value>
// </Param>
// Zero param elements is okay
XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/Param" );
if (!nset )
{
success = TRUE;
return;
}
INTEGER ncnt = nset.getLength();
INTEGER i;
XmlElement xe;
BOOLEAN suc;
for (i=0; i<ncnt; i++)
{
xe = nset.item ( index: i );
success = this.appendParam ( xelem: xe );
if ( success == FALSE )
{
return;
}
}
success = TRUE;
}
private METHOD parseReturns ( XPath xpath ) RETURNS ( BOOLEAN success )
{
// Example
// <Return ArgType="Integer" ArgLength="1">
// <Name>aParam</Name>
// </Return>
// Zero param elements is okay
XmlNodeSet nset = xpath.evaluate( expr: "/RunXIP/Input/ExpectedReturnVal" );
if (!nset )
{
success = TRUE;
return;
}
INTEGER ncnt = nset.getLength();
INTEGER i;
XmlElement xe;
BOOLEAN suc;
for (i=0; i<ncnt; i++)
{
xe = nset.item ( index: i );
success = this.appendReturn ( xelem: xe );
if ( success == FALSE )
{
return;
}
}
success = TRUE;
}
private METHOD appendParam ( XmlElement xelem ) RETURNS ( BOOLEAN success )
{
// Example - this is a list of integers
// <Param ArgType="Integer">
// <Name>aParam</Name>
// <Value>27</Value>
// <Value>43</Value>
// </Param>
STRING datatype = xelem.getAttribute( name: "ArgType" );
STRING listTest = xelem.getAttribute( name: "IsList" );
LIST vals = this.fetchChildrenByName ( parent: xelem, cName: "Value" );
XmlElement nm = this.fetchChild ( parent: xelem, cName: "Name" );
INTEGER dataCnt = vals.length();
XmlElement val;
if ((dataCnt > 1) || (listTest.strcasecmp(str: "true") == TRUE)) /* this is a collection */
{
INTEGER i;
LIST collect;
STRING aName = this.innerText( xelem: nm );
for (i=o; i<dataCnt; i=i+1)
{
val = vals.ref( entry: i );
this.insertEntryByType (
obj: this.innerText( xelem: val ),
atloc: 10000,
oftype: datatype,
intolist: collect);
}
this.insertNameByType (
obj: collect,
ofname: this.innerText( xelem: nm ),
oftype: "list",
intolist: this.params);
}
else
{
val = vals.ref( entry: 0 );
this.insertNameByType (
obj: this.innerText( xelem: val ),
ofname: this.innerText( xelem: nm ),
oftype: datatype,
intolist: this.params);
}
success = TRUE;
}
private METHOD insertNameByType (OBJECT obj, STRING ofname, STRING oftype, LIST intolist)
{
INTEGER iDt;
DOUBLE dDt;
BOOLEAN bDt;
STRING sDt;
LIST lDt;
if ( oftype.strcasecmp( str: "integer" ) == TRUE )
{
iDt = obj; // cast to integer
intolist.insert( name: ofname, obj: iDt );
}
else if ( oftype.strcasecmp( str: "string" ) == TRUE )
{
sDt = obj; // cast to string
intolist.insert( name: ofname, obj: sDt );
}
else if ( oftype.strcasecmp( str: "double" ) == TRUE )
{
dDt = obj; // cast to double
intolist.insert( name: ofname, obj: dDt );
}
else if ( oftype.strcasecmp( str: "boolean" ) == TRUE )
{
if (obj.strcasecmp(str: "false"))
bDt = FALSE;
else if ((obj.strcasecmp(str: "true")) || (obj == 1))
bDt = TRUE;
else
bDt = FALSE;
intolist.insert( name: ofname, obj: bDt );
}
else if ( oftype.strcasecmp( str: "list" ) == TRUE )
{
lDt = obj; // cast to LIST
intolist.insert( name: ofname, obj: lDt );
}
}
private METHOD insertEntryByType (OBJECT obj, INTEGER atloc, STRING oftype, LIST intolist)
{
INTEGER iDt;
DOUBLE dDt;
BOOLEAN bDt;
STRING sDt;
LIST lDt;
if ( oftype.strcasecmp( str: "integer" ) == TRUE )
{
iDt = obj; // cast to integer
intolist.insert( entry: atloc, obj: iDt );
}
else if ( oftype.strcasecmp( str: "string" ) == TRUE )
{
sDt = obj; // cast to string
intolist.insert( entry: atloc, obj: sDt );
}
else if ( oftype.strcasecmp( str: "double" ) == TRUE )
{
dDt = obj; // cast to double
intolist.insert( entry: atloc, obj: dDt );
}
else if ( oftype.strcasecmp( str: "boolean" ) == TRUE )
{
if (obj.strcasecmp(str: "false"))
bDt = FALSE;
else if ((obj.strcasecmp(str: "true")) || (obj == 1))
bDt = TRUE;
else
bDt = FALSE;
intolist.insert( entry: atloc, obj: bDt );
}
else if ( oftype.strcasecmp( str: "list" ) == TRUE )
{
lDt = obj; // cast to LIST
intolist.insert( entry: atloc, obj: lDt );
}
}
private METHOD appendReturn ( XmlElement xelem ) RETURNS ( BOOLEAN success )
{
// Example
// <Return ArgType="Integer" ArgLength="1">
// <Name>aParam</Name>
// </Return>
INTEGER iDt;
DOUBLE dDt;
BOOLEAN bDt;
STRING datatype = xelem.getAttribute( name: "ArgType" );
STRING dataCnt = 1;
if ( xelem.hasAttribute( name: "AgrLength" ) == TRUE)
dataCnt = xelem.getAttribute( name: "ArgLength" );
XmlElement nm = this.fetchChild ( parent: xelem, cName: "Name" );
STRING inText = this.innerText( xelem: nm );
this.retVals.insert( name: inText, obj: 1 );
this.retTypes.insert( obj: datatype );
this.retNames.insert( obj: inText );
success = TRUE;
}
private METHOD fetchChildrenByName (XmlNode parent, STRING cName) RETURNS (LIST kids)
{
XmlNodeList nl = parent.getChildNodes();
INTEGER icnt = nl.getLength();
INTEGER i;
XmlNode member;
XmlElement xe;
for (i=0; i<icnt; i++)
{
member = nl.item( index: i );
if (member.getNodeType() == XmlNode.XML_ELEMENT_NODE)
{
xe = member;
if ( xe.getTagName().strcasecmp( str: cName ) == TRUE )
{
kids.insert(entry: 10000, obj: xe);
}
}
}
}
private METHOD fetchChild (XmlNode parent, STRING cName) RETURNS (XmlNode elem)
{
XmlNodeList nl = parent.getChildNodes();
elem = this.fetchElement ( nList: nl, eName: cName );
}
/* Gets the first element matching the name */
private METHOD fetchElement (XmlNodeList nList, STRING eName) RETURNS (XmlNode elem)
{
INTEGER icnt = nList.getLength();
INTEGER i;
XmlNode member;
XmlElement xe;
for (i=0; i<icnt; i++)
{
member = nList.item( index: i );
if (member.getNodeType() == XmlNode.XML_ELEMENT_NODE)
{
xe = member;
if ( xe.getTagName().strcasecmp( str: eName ) == TRUE )
{
elem = member;
return;
}
}
}
}
private METHOD innerText( XmlNode xelem ) RETURNS ( STRING str )
{
XmlText xt = xelem.getFirstChild();
if (!xt)
{
SetStatus( op: "stop", msg: "XIPRunClass: Not able to get Inner Text" );
this.isValid = FALSE;
return;
}
str = xt.getData();
}
private METHOD printChildNodes( xmlNode target )
{
XmlNodeList nList = target.getChildNodes();
if (!nList)
{
print "No child nodes found for "+target.getNodeName();
}
this.printNodeList( nList: nList );
}
private METHOD printNodeList ( XmlNodeList nList )
{
INTEGER icnt = nList.getLength();
INTEGER i;
XmlNode member;
print "Node list member count: "+icnt;
for (i=0; i<icnt; i++)
{
member = nList.item( index: i );
this.printNode( target: member );
}
}
private METHOD printNode ( xmlNode target )
{
INTEGER typeid = target.getNodeType();
XmlText xt;
XmlElement xe;
if (typeid == XmlNode.XML_TEXT_NODE)
{
xt = target;
print "text data is ";
print xt.getData()+" with length "+xt.getLength();
}
else if (typeid == XmlNode.XML_ELEMENT_NODE)
{
xe = target;
print "Element name is ";
print xe.getTagName();
}
else
{
print "XmlNode "+target.getNodeName()+" of type "+typeid+" not known in printNode";
}
}
/*****************************************************************************
******************** Internal Data Fields ******************************
*****************************************************************************/
LIST params;
LIST retVals;
LIST retNames;
LIST retTypes;
STRING name = "Not Done";
STRING scriptValue;
BOOLEAN isValid = FALSE;
BOOLEAN isBlock = FALSE;
BOOLEAN isVocal = FALSE;
}